using OpenIddict.Abstractions;

namespace WL.Think.Abp;

public class ThinkDbMigratorOptions
{
    public static readonly string NodeName = "Think";

    /// <summary>
    /// 启用多租户
    /// </summary>
    public bool MultiTenancy { get; set; } = false;

    /// <summary>
    /// 启动类模块
    /// </summary>
    public Type? StartupModuleType { get; set; }

    /// <summary>
    /// 作用域列表
    /// </summary>
    public List<ThinkOpenIddictScope>? Scopes { get; set; }

    /// <summary>
    /// 应用列表
    /// </summary>
    public List<ThinkOpenIddictApplication>? Applications { get; set; }
}


/// <summary>
/// OpenIddict 应用
/// </summary>
public class ThinkOpenIddictApplication
{
    /// <summary>
    /// 客户端ID 或者 名称
    /// </summary>
    public string? ClientId { get; set; }

    /// <summary>
    /// 客户端秘钥
    /// </summary>
    public string? Secret { get; set; }

    /// <summary>
    /// 客户端根地址
    /// </summary>
    public string? ClientUri { get; set; }

    /// <summary>
    /// 客户端显示名称，没有配置使用ClientId
    /// </summary>
    public string? DisplayName { get; set; }

    /// <summary>
    /// 客户端可以访问的范围
    /// </summary>
    public List<string> Scopes { get; set; } = [];

    /// <summary>
    /// 应用类型
    /// </summary>
    public string ApplicationType { get; set; } = OpenIddictConstants.ApplicationTypes.Web;

    /// <summary>
    /// 客户端类型
    /// </summary>
    public string Type { get; set; } = OpenIddictConstants.ClientTypes.Public;

    /// <summary>
    /// 许可类型
    /// </summary>
    public string ConsentType { get; set; } = OpenIddictConstants.ConsentTypes.Implicit;

    /// <summary>
    /// 授权类型
    /// </summary>
    public List<string> GrantTypes { get; set; } = [];

    /// <summary>
    /// 登录成功后返回的客户端地址
    /// </summary>
    public List<string>? RedirectUris { get; set; }

    /// <summary>
    /// 注销登录后返回的客户端地址
    /// </summary>
    public List<string>? PostLogoutRedirectUris { get; set; }

    /// <summary>
    /// 图标地址
    /// </summary>
    public string? LogoUri { get; set; } 


    /// <summary>
    /// 获取Scope
    /// </summary>
    /// <returns></returns>
    public List<string> GetScopesWithDefault()
    {
        var list = new List<string> { 
            OpenIddictConstants.Permissions.Scopes.Address,
            OpenIddictConstants.Permissions.Scopes.Email,
            OpenIddictConstants.Permissions.Scopes.Phone,
            OpenIddictConstants.Permissions.Scopes.Profile,
            OpenIddictConstants.Permissions.Scopes.Roles,
        };

        Scopes.ForEach(x => list.AddIfNotContains(x));
        return list;
    }

    /// <summary>
    /// 获取授权类型列表
    /// </summary>
    /// <returns></returns>
    public List<string> GetGrantTypesWithDefault()
    {
        var list = new List<string> {
            OpenIddictConstants.GrantTypes.AuthorizationCode,
            OpenIddictConstants.GrantTypes.Password,
            OpenIddictConstants.GrantTypes.ClientCredentials,
            OpenIddictConstants.GrantTypes.RefreshToken,
            "LinkLogin",
            "Impersonation",
        };

        GrantTypes.ForEach(x=> list.AddIfNotContains(x));
        return list;
    }
}

/// <summary>
/// 作用域
/// </summary>
public class ThinkOpenIddictScope
{
    /// <summary>
    /// Scope名称
    /// </summary>
    public string? Name { get; set; }
    /// <summary>
    /// Scope显示名称
    /// </summary>
    public string? DisplayName { get; set; }
    /// <summary>
    /// 资源
    /// </summary>
    public HashSet<string> Resources { get; set; } = new HashSet<string>();
}
